using System;
using FireStarter.Base.BusinessObjectMerger;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;

namespace FireStarterModeller.Logic.MergeBusinessObjects
{
    //Used by the BOMergeableBORegistry
// ReSharper disable UnusedMember.Global 
    public class MergeDMRelationship :MergeBO<DMRelationship> 
    {
        protected override void MergeMultipleRelationship(IMultipleRelationship fromMultipleRelationship, IMultipleRelationship toMultipleRelationship)
        {
            //Remove all the RelProps for the Relationship so that they can be created from scratch from the fromRelationship.
            //Since there is no Props of the RelProps to match and find from.
            if (fromMultipleRelationship.RelationshipName == "RelationshipProperties")
            {
                while(toMultipleRelationship.CurrentBusinessObjectCollection.Count > 0)
                {
                    var relProp = toMultipleRelationship.CurrentBusinessObjectCollection[0];
                    relProp.MarkForDelete();
                }
            }
            base.MergeMultipleRelationship(fromMultipleRelationship,toMultipleRelationship);
        }

        public override DMRelationship CreateBusinessObject()
        {
            return new DMRelationship(null);
        }
        protected override void MergeProperties(IBusinessObject fromMergableBo, IBusinessObject toMergableBo)
        {
            base.MergeProperties(fromMergableBo, toMergableBo);
            DMRelationship fromDMRel = (DMRelationship) fromMergableBo;
            DMRelationship toDMRel = (DMRelationship) toMergableBo;
            toDMRel.ReverseRelationshipName = fromDMRel.ReverseRelationshipName;
            toDMRel.RelatedClassName = fromDMRel.RelatedClassName;
            toDMRel.RelatedAssemblyName = fromDMRel.RelatedAssemblyName;
        }

        public override Func<DMRelationship, bool> MatchBusinessObject(DMRelationship boToFind)
        {
            return foundBO => foundBO.RelationshipName == boToFind.RelationshipName 
                    && foundBO.RelatedClassName == boToFind.RelatedClassName;
        }
    }
    public class MergeDMRelationshipProperty :MergeBO<DMRelationshipProperty> 
    {
        public override DMRelationshipProperty CreateBusinessObject()
        {
            return new DMRelationshipProperty(null);
        }

        protected override void MergeProperties(IBusinessObject fromMergableBo, IBusinessObject toMergableBo)
        {
            base.MergeProperties(fromMergableBo, toMergableBo);
            IDMRelationshipProperty fromDMRelProp = (IDMRelationshipProperty)fromMergableBo;
            IDMRelationshipProperty toDMRelProp = (IDMRelationshipProperty)toMergableBo;
            toDMRelProp.RelatedPropertyName = fromDMRelProp.RelatedPropertyName;
            toDMRelProp.OwnerPropertyName = fromDMRelProp.OwnerPropertyName;
        }
    }
    // ReSharper restore UnusedMember.Global
}